package com.yaozheng.vocationaltraining.service.impl.limited;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.limited.ITimedPaperView;
import com.yaozheng.vocationaltraining.service.limited.TimedPaperService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

/**
 * 极限挑战
 */
@EBean
public class TimedPaperServiceImpl implements TimedPaperService {
    ITimedPaperView iTimedPaperView;

    @Override
    public void init(ITimedPaperView iTimedPaperView) {
        this.iTimedPaperView = iTimedPaperView;
    }

    @Background
    @Override
    public void createPapper(int paperId) {
        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/exam/race/" + paperId,
                    iTimedPaperView.getToken(), new BaseView(iTimedPaperView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTimedPaperView.createPapperSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iTimedPaperView.createPapperError(error);
                        }

                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTimedPaperView.isResponseResult()) {
                iTimedPaperView
                        .createPapperError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }
}
